package com.example.ims.fame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.springframework.context.ApplicationContext;

import com.example.ims.pojo.Customer;
import com.example.ims.service.CustomerService;

public class AddCustomerFame extends JDialog {
    private JTextField idTextField;
    private JTextField nameTextField;
    private JTextField contactTextField;
    private JTextField phoneTextField;
    private JTextField addressTextField;
    private JTextField creditTextField;
    private CustomerService customerService;
    private ApplicationContext context;

    public AddCustomerFame(Frame owner, String title, boolean modal, ApplicationContext context) {
        super(owner, title, modal);
        this.context = context;
        customerService = context.getBean(CustomerService.class);
        initComponents();
    }

    private void initComponents() {
        setTitle("增加客户");
        setModal(true);
        setLayout(new BorderLayout());

        // 整体内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayout(6, 2));

        // 客户名称输入框及标签
        JLabel nameLabel = new JLabel("客户名称:");
        nameTextField = new JTextField();
        contentPanel.add(nameLabel);
        contentPanel.add(nameTextField);

        // 联系人输入框及标签
        JLabel contactLabel = new JLabel("联系人:");
        contactTextField = new JTextField();
        contentPanel.add(contactLabel);
        contentPanel.add(contactTextField);

        // 联系电话输入框及标签
        JLabel phoneLabel = new JLabel("联系电话:");
        phoneTextField = new JTextField();
        contentPanel.add(phoneLabel);
        contentPanel.add(phoneTextField);

        // 地址输入框及标签
        JLabel addressLabel = new JLabel("地址:");
        addressTextField = new JTextField();
        contentPanel.add(addressLabel);
        contentPanel.add(addressTextField);

        // 信用额度输入框及标签
        JLabel creditLabel = new JLabel("信用额度:");
        creditTextField = new JTextField();
        contentPanel.add(creditLabel);
        contentPanel.add(creditTextField);

        // 确认和取消按钮面板
        JPanel buttonPanel = new JPanel();
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的值
                String name = nameTextField.getText();
                String contact = contactTextField.getText();
                String phone = phoneTextField.getText();
                String address = addressTextField.getText();
                String credit = creditTextField.getText();

                try {
                    // 创建Customer对象并设置属性
                    Customer customer = new Customer();
                    customer.setName(name);
                    customer.setContact(contact);
                    customer.setPhone(phone);
                    customer.setAddress(address);
                    customer.setCreditLimit(Integer.parseInt(credit));

                    // 调用添加客户业务逻辑方法
                    customerService.addCustomer(customer);

                    // 弹出提示框告知用户添加成功
                    JOptionPane.showMessageDialog(AddCustomerFame.this, "客户添加成功", "添加提示", JOptionPane.INFORMATION_MESSAGE);
                    dispose(); // 关闭对话框
                } catch (NumberFormatException ex) {
                    // 如果信用额度等需要数字格式的输入有误，弹出错误提示框
                    JOptionPane.showMessageDialog(AddCustomerFame.this, "信用额度输入的数字格式不正确，请检查", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (Exception ex) {
                    // 其他异常，弹出相应错误提示框
                    JOptionPane.showMessageDialog(AddCustomerFame.this, "客户添加失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭对话框
            }
        });

        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);

        add(contentPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置对话框大小并使其显示在相对合适的位置（相对父窗口）
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
    }
}